/**
 * 
 */
package ro.utcn.tp.taskManagement.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;

import ro.utcn.tp.taskManagement.business.ImageService;
import ro.utcn.tp.taskManagement.business.TasksManager;
import ro.utcn.tp.taskManagement.model.ImageDefs;
import ro.utcn.tp.taskManagement.model.ImagePanel;
import ro.utcn.tp.taskManagement.model.Task;
import ro.utcn.tp.taskManagement.model.User;
import ro.utcn.tp.taskManagement.utils.ExcelExporter;

/**
 * @author Coni
 * 
 */
public class ExportPage implements ActionListener {

	private JLabel jlbTitlu;

	private JLabel jlbSelectExportType;

	private JLabel jlbSelectPath;

	private JLabel jlbFileName;

	private JTextField jtfSelectedPath;

	private JTextField jtfFileName;

	private JButton jbnExport;
	private JButton jbnBrowse;
	// private JFileChooser jbnBrowse;

	private JButton jbnCancel;

	private JComboBox<String> jcbSelectExportType;

	private Container container;

	/** A reference to the image service. */

	private ImageService imageService;// pentru a tine imaginile

	private static ExportPage instance;
	private User user;

	/**
	 * The constructor of the class. initializes the screen.
	 */
	public ExportPage(User user) {
		this.user = user;
		imageService = ImageService.getInstance();// instantiez imageservice
		createGUI();
	}

	// imagine de background
	public void createGUI() {

		container = new ImagePanel(
				imageService.getImage(ImageDefs.APPLICATION_BACKGROUND_PATH));
		container.setLayout(new GridBagLayout());
		arrangeComponents();
	}

	public void arrangeComponents() {
		jlbTitlu = new JLabel("Export tasks");

		jlbSelectExportType = new JLabel("Select export type:");
		jlbSelectPath = new JLabel("Select path:");
		jlbFileName = new JLabel("File name:");
		jcbSelectExportType = new JComboBox<String>();
		fillTypesComboBox();

		jtfSelectedPath = new JTextField(50);
		jtfSelectedPath.setEditable(true);

		jtfFileName = new JTextField("");
		jtfFileName.setEditable(true);

		jbnBrowse = new JButton("Browse...");
		jbnExport = new JButton("Export");
		jbnCancel = new JButton("Cancel");

		// jbnBrowse.setCurrentDirectory(new java.io.File("."));
		// jbnBrowse.setDialogTitle("Browse");
		// jbnBrowse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		//
		// disable the "All files" option.
		//
		// jbnBrowse.setAcceptAllFileFilterUsed(false);
		Font font = new Font("Times New Roman", Font.ITALIC, 30);
		jlbTitlu.setFont(font);

		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
		gridBagConstraintsx01.gridx = 0;
		gridBagConstraintsx01.gridy = 0;
		gridBagConstraintsx01.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx01.gridwidth = 3;
		gridBagConstraintsx01.fill = GridBagConstraints.BOTH;
		container.add(jlbTitlu, gridBagConstraintsx01);

		GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
		gridBagConstraintsx02.gridx = 0;
		gridBagConstraintsx02.gridy = 3;
		gridBagConstraintsx02.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx02.gridwidth = 1;
		gridBagConstraintsx02.anchor = GridBagConstraints.WEST;
		container.add(jlbSelectExportType, gridBagConstraintsx02);

		GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
		gridBagConstraintsx03.gridx = 1;
		gridBagConstraintsx03.gridy = 3;
		gridBagConstraintsx03.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx03.gridwidth = 1;
		gridBagConstraintsx03.gridheight = 1;
		gridBagConstraintsx03.fill = GridBagConstraints.BOTH;
		container.add(jcbSelectExportType, gridBagConstraintsx03);

		GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
		gridBagConstraintsx04.gridx = 0;
		gridBagConstraintsx04.gridy = 1;
		gridBagConstraintsx04.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx04.gridwidth = 1;
		gridBagConstraintsx04.anchor = GridBagConstraints.WEST;
		container.add(jlbSelectPath, gridBagConstraintsx04);

		GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
		gridBagConstraintsx05.gridx = 1;
		gridBagConstraintsx05.gridy = 1;
		gridBagConstraintsx05.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx05.gridwidth = 2;
		gridBagConstraintsx05.gridheight = 1;
		gridBagConstraintsx05.fill = GridBagConstraints.BOTH;
		container.add(jtfSelectedPath, gridBagConstraintsx05);

		GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
		gridBagConstraintsx06.gridx = 3;
		gridBagConstraintsx06.gridy = 1;
		gridBagConstraintsx06.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx06.gridwidth = 1;
		gridBagConstraintsx06.fill = GridBagConstraints.BOTH;
		container.add(jbnBrowse, gridBagConstraintsx06);

		GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
		gridBagConstraintsx09.gridx = 0;
		gridBagConstraintsx09.gridy = 2;
		gridBagConstraintsx09.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx09.gridwidth = 1;
		gridBagConstraintsx09.anchor = GridBagConstraints.WEST;
		container.add(jlbFileName, gridBagConstraintsx09);

		GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
		gridBagConstraintsx10.gridx = 1;
		gridBagConstraintsx10.gridy = 2;
		gridBagConstraintsx10.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx10.gridwidth = 2;
		gridBagConstraintsx10.gridheight = 1;
		gridBagConstraintsx10.fill = GridBagConstraints.BOTH;
		container.add(jtfFileName, gridBagConstraintsx10);

		GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
		gridBagConstraintsx07.gridx = 0;
		gridBagConstraintsx07.gridy = 4;
		gridBagConstraintsx07.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx07.gridwidth = 1;
		gridBagConstraintsx07.fill = GridBagConstraints.BOTH;
		container.add(jbnExport, gridBagConstraintsx07);

		GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
		gridBagConstraintsx08.gridx = 1;
		gridBagConstraintsx08.gridy = 4;
		gridBagConstraintsx08.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx08.gridwidth = 1;
		gridBagConstraintsx08.fill = GridBagConstraints.BOTH;
		container.add(jbnCancel, gridBagConstraintsx08);

		jbnBrowse.setEnabled(true);
		jbnCancel.setEnabled(true);
		jbnExport.setEnabled(true);
		jbnBrowse.addActionListener(this);
		jbnCancel.addActionListener(this);
		jbnExport.addActionListener(this);
		jbnBrowse
				.setToolTipText("Select the directory path to export the tasks");

	}

	/**
	 * populez comboBox
	 */
	private void fillTypesComboBox() {
		jcbSelectExportType.addItem(".xls");
		jcbSelectExportType.addItem(".pdf");
		jcbSelectExportType.addItem(".txt");
	}

	public void stateChanged(ChangeEvent arg0) {

	}

	public static ExportPage getNewInstance(User user) {
		instance = new ExportPage(user);
		return instance;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		try {
			if (e.getSource() == jbnBrowse) {
				browse();
			} else if (e.getSource() == jbnExport) {
				export();
			} else if (e.getSource() == jbnCancel) {
				cancel();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void export() throws Exception {
		int selectedExportType = jcbSelectExportType.getSelectedIndex();
		List<Task> tasks = TasksManager.getInstance().getUserTasks(
						user.getId());
		// TODO validate path and file name!!!!
		switch (selectedExportType) {
		case 0:
			
			String excelOutputFile = jtfSelectedPath.getText() + File.separator + jtfFileName.getText();
			ExcelExporter.exportToExcel(excelOutputFile, tasks);

			break;
		case 1:
			/*
			 * Document document = new Document(); try { PdfWriter writer =
			 * PdfWriter.getInstance(document, new
			 * FileOutputStream("D:/jTable.pdf"));
			 * 
			 * document.open(); PdfContentByte cb = writer.getDirectContent();
			 * 
			 * cb.saveState();
			 * 
			 * PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(),
			 * table.getHeight()); Graphics2D g2 =
			 * pdfTemplate.createGraphics(table.getWidth(), table.getHeight());
			 * 
			 * table.print(g2);
			 * 
			 * cb.addTemplate(pdfTemplate, 20, 20); g2.dispose();
			 * cb.restoreState(); } catch (Exception e) {
			 * System.err.println(e.getMessage()); } document.close();
			 */
			break;
		case 2:
			try {
				File newTextFile = new File(jtfSelectedPath.getText() + "/"
						+ jtfFileName.getText());
				FileWriter fw = new FileWriter(newTextFile);
		
				Iterator<Task> iterator = tasks.iterator();
				while (iterator.hasNext()) {
					Task task = iterator.next();

					fw.write("Task description:");
					fw.write(task.getDescription());
					fw.write("   Status:");
					fw.write(task.getStatus());
					fw.write("\n");
				}

				fw.close();
			} catch (IOException iox) {
				iox.printStackTrace();
			}
			break;
		}

	}

	private void browse() {
		// BrowseScreen.getInstance(user);
		JFileChooser jfcBrowse = new JFileChooser("Browse");
		jfcBrowse.setCurrentDirectory(new java.io.File("."));
		jfcBrowse.setDialogTitle("Browse");
		jfcBrowse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		//
		// disable the "All files" option.
		//
		jfcBrowse.setAcceptAllFileFilterUsed(false);
		if (jfcBrowse.showOpenDialog(container) == JFileChooser.APPROVE_OPTION) {

			String selectedFolder = jfcBrowse.getSelectedFile()
					.getAbsolutePath();
			jtfSelectedPath.setText(selectedFolder);
		}

	}

	private void cancel() {
		ApplicationScreen.getInstance().setContainer(
				UserPage.getInstance().getContainer());
	}

	/**
	 * @return
	 */
	public Container getContainer() {
		return container;
	}

}
